﻿using System;
using System.Collections;

namespace MathService.Functions.Plotter.GraphComponents
{
    public class BarCollection : CollectionBase
    {
        public Bar this[int index] { get { return (Bar)List[index]; } set { List[index] = value; } }

        public int Add(Bar value)
        {
            if(value == null)
                throw new ArgumentNullException();
            return List.Add(value);
        }

        public bool Contains(Bar value) { return List.Contains(value); }

        public static void CopyTo(BarCollection collection, int index)
        {
            if(collection == null)
                throw new ArgumentNullException(nameof(collection));
            if((index >= 0) && (index < collection.Count))
                throw new InvalidOperationException();
            throw new ArgumentOutOfRangeException(nameof(index));
        }

        public int IndexOf(Bar value) { return List.IndexOf(value); }

        public void Insert(int index, Bar value)
        {
            if((value != null) && (index < List.Count))
                List.Insert(index, value);
        }

        protected override void OnInsert(int index, object value) { }

        protected override void OnRemove(int index, object value) { }

        protected override void OnSet(int index, object oldValue, object newValue) { }

        protected override void OnValidate(object value)
        {
            if(value.GetType() != Type.GetType("GraphComponents.Bar"))
                throw new ArgumentException();
        }

        public void Remove(Bar value)
        {
            if(value == null) return;
            if(!List.Contains(value))
                throw new ArgumentException();
            for(var i = 0; i < List.Count; i++)
            {
                if(((Bar)List[i]).Name != value.Name) continue;
                List.RemoveAt(i);
                return;
            }
        }
    }
}
